_package("alz.mui");

_import("alz.mui.ActionElement");

/**
 * 列表项目
 */
_class("ListItem", ActionElement, function(){
	this._init = function(){
		_super._init.call(this);
		this._checkbox = null;
		//this._timer = 0;
	};
	this.create = function(parent, data, tpl){
		if(parent) this.setParent(parent);
		if(data) this.setData(data);
		var html = runtime.formatTpl(tpl, data, parent.createHash(data));
		var obj = this.createDomElement(parent, html);
		this.init(obj);
		return obj;
	};
	this.bind = function(obj, data, parent){
		if(parent) this.setParent(parent);
		if(data) this.setData(data);
		this.init(obj);
	};
	this.init = function(obj){
		_super.init.apply(this, arguments);
		if(this.getAction()){
			this.addListener(this._self, "tapstart", this, "onTapStart");
			this.addListener(this._self, "tapcancel", this, "onTapCancel");
			this.addListener(this._self, "tap", this, "onTap");
		}
		/*
		if(!runtime.isTouch){
			this._self.onclick = function(ev){
				if(this.disabled) return;
				//ev.stopPropagation();
				//this._ptr.getParent().fireEvent({"type": "itemChange", "sender": this, "item": this._ptr});
				return this._ptr.dispatchAction(this, ev);
			};
			runtime.addGlobalListener(this._self, "mouseup", this, function(ev){
				if(this._self.disabled) return;
				//runtime.stopTimer(this._timer);
				this.setState("normal");
			});
		}
		*/
	};
	this.dispose = function(){
		if(this._disposed) return;
		if(this._checkbox){
			this._checkbox.onclick = null;
			this._checkbox.onmousedown = null;
			this._checkbox = null;
		}
		/*
		if(!runtime.isTouch){
			runtime.removeGlobalListener(this._self, "mouseup", this);
			this._self.onclick = null;
		}
		*/
		_super.dispose.apply(this);
	};
	this.setCheckbox = function(v){
		if(v){
			this._checkbox = v;
			this._checkbox.onmousedown = function(ev){
				//ev = ev || window.event;
				ev.stopPropagation();
			};
			this._checkbox.onclick = function(ev){
				//ev = ev || window.event;
				ev.stopPropagation();
			};
		}
	};
	this.setState = function(v){
		switch(v){
		case "active":
			runtime._element.addClass(this._self, "cur");
			break;
		case "normal":
		default:
			runtime._element.removeClass(this._self, "cur");
			break;
		}
	};
	this.activate = function(){
		//this._self.className = "active";
		if(this._checkbox){
			this._checkbox.checked = true;
		}
		this.setState("active");
	};
	this.deactivate = function(){
		//this._self.className = "";
		if(this._checkbox){
			this._checkbox.checked = false;
		}
		this.setState("normal");
	};
	this.onTapStart = function(ev){
		//runtime.log("ListItem::onTapStart");
		if(this._self.disabled) return;
		this.getParent().activeItem(this);
	};
	this.onTapCancel = function(ev){
		//runtime.log("ListItem::onTapCancel");
		this.getParent().activeItem(null);
	};
	this.onTap = function(ev){
		//runtime.log("ListItem::onTap");
		if(this._self.disabled) return;
		this.getParent().activeItem(null);
		var ret;
		//ret = this.onAction(ev);
		//if(runtime.isTouch){
			ret = this.dispatchAction(this._self, ev);  //使用touch事件模拟onclick
		//}else{
		//	ret = false;
		//}
		//runtime.releaseCapture(this._self);
		return ret;
	};
	this.updateView = function(data){
	};
});